package com.t309.tzy.controller;
import com.alibaba.fastjson.JSON;
import com.t309.tzy.common.WebResult;
import com.t309.tzy.entity.TSysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;

//@RestController //将所有handler都定义为响应体 @ResponseBody
@Controller //将该类注入到spring容器, 并定义成控制器
@RequestMapping ( "/test" ) //通过请求进行 mapping映射
public class TestController extends AbstractController {
    private Logger log = Logger.getLogger("TestController");

    //public String


    @ResponseBody//响应体,省略返回视图渲染, 直接返回给用户
    @GetMapping( "/json" )
    //@RequestMapping( value="/insert",method = RequestMethod.GET )//处理器
    public Object json(){

        HashMap<String,Object> map = new HashMap<>();
        map.put("key",1);
        log.info("JSON.toJSONString(map)="+JSON.toJSONString(map));


        return JSON.toJSONString(map);
    }

    @ResponseBody//响应体,省略返回视图渲染, 直接返回给用户
    @GetMapping( "/json2" )
    //@RequestMapping( value="/insert",method = RequestMethod.GET )//处理器
    public Object json2(){
        TSysUser user = new TSysUser();
        user.setAccount("123124");
        user.setRealName("张三");
        user.setCreatedTime(new Date());
        WebResult result = new WebResult();
        result.setCode(1);
        result.setMsg("张三");

        result.putData("多个","官方的")
                .putData("阿","规范化")
                .putData("波","白板笔");
        log.info("JSON.toJSONString(user)="+JSON.toJSONString(user));
        log.info("JSON.toJSONString(result)="+JSON.toJSONString(result));

        //return JSON.toJSON(result);
        return user;
    }

    @ResponseBody//响应体,省略返回视图渲染, 直接返回给用户
    @GetMapping( "/json3" )
    //@RequestMapping( value="/insert",method = RequestMethod.GET )//处理器
    public Object json3(){
        TSysUser user = new TSysUser();
        user.setAccount("123124");
        user.setRealName("张三");
        user.setCreatedTime(new Date());
        WebResult result = new WebResult();
        result.setCode(1);
        result.setMsg("张三");

        result.putData("多个","官方的")
                .putData("阿","规范化")
                .putData("波","白板笔");
        //log.info("JSON.toJSONString(user)="+JSON.toJSONString(user));
        log.info("JSON.toJSONString(result)="+JSON.toJSONString(result));

        //return JSON.toJSON(result);
        return result;
    }


    @GetMapping( "/test3" )
    //@RequestMapping( value="/insert",method = RequestMethod.GET )//处理器
    public String hello(){
        log.info("==新增功能==");


        return "insert";
    }
    @GetMapping( "/insert" )
    //@RequestMapping( value="/insert",method = RequestMethod.GET )//处理器
    public String insert(){
        log.info("==新增功能==");


        return "insert";
    }

    @Override
    @GetMapping("/test")
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //添加属性
        modelAndView.addObject("name","tzyjhdgsd");
        //视图名称:jsp页面名称
        modelAndView.setViewName("/test");
        System.out.println("handleRequestInternal()  Spring MVC框架搭建成功 !!!");
        //return modelAndView;
        return modelAndView;
    }
}
